﻿using NUnit.Framework;
using System.Drawing;
using Demo.Test.JosnTest;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;


namespace Demo.Test
{
    /// <summary>
    /// json序列化相关知识
    /// </summary>
    public class JsonToSerialize
    {
        [SetUp]
        public void Setup()
        {

        }

        /// <summary>
        /// json序列化枚举
        /// </summary>
        [Test]
        public void Test1()
        {
            //var options = new JsonSerializerOptions();
            //options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
            var json = System.Text.Json.JsonSerializer.Serialize(new Circle
            {
                Radius = 10.1,
                BgColor = Color.Black
            });
            var result = "{ \"Radius\": 10.1, \"BgColor\": \"Black\" }";
            Assert.Pass(result, json);
        }


        /// <summary>
        /// 自动序列化json枚举为参数描述
        /// </summary>
        [Test]
        public void Test2()
        {
            var json = JsonConvert.SerializeObject(new TShirt { Name = "男士衬衫",
                ColorScheme = ColorScheme.WhiteBlue }, new StringEnumConverter());
            var result = "{\"Name\":\"男士衬衫\",\"ColorScheme\":\"白/蓝\"}";
            Assert.AreEqual(result, json);
        }
    }
}
